import urls from '/util/urls'
import request from '/util/request'
import { alert } from '/util/utils'

Page({
  data: {
    sId:'',
    // ID
    id:'',
    // 编码
    code: '',
    // 开始时间
    startTime: '',
    // 结束时间
    endTime: '',
    // 表达式
    cronExp: '',
    // 执行人编码
    executorCode: '',
    // 执行人名称
    executorName: '',
    // 单位编码
    unitCode: '',
    // 问题
    problems: '',
    // 解决方案
    solveSituation: '',
    // 状态
    status: '',
    // 类型
    type: '',
    // 巡检任务详细
    inspectionTaskItem: [],
    // 巡检类型
    inspectionType: {},
  },
  onLoad(options) {
    let that = this
    // 传递的参数
    const id = options.id
    // 加载巡检的详细信息
    request.get(urls.inspectionServiceDetailUrl(id,'after'), function(data){
      that.setData({
        sId:id,
         // ID
        id: id,
        // 编码
        code: data.code,
        // 开始时间
        startTime: data.startTime,
        // 结束时间
        endTime: data.endTime,
        // 表达式
        cronExp: data.cronExp,
        // 执行人编码
        executorCode: data.executorCode,
        // 执行人名称
        executorName: data.executorName,
        // 单位编码
        unitCode: data.unitCode,
        // 问题
        problems: data.problems,
        // 解决方案
        solveSituation: data.solveSituation,
        // 状态
        status: data.status,
        // 类型
        type: data.type,
        // 巡检任务详细
        inspectionTaskItem: data.inspectionTaskItem.map(it => {
          if (it.item.dataType === 'select' && data.status === 'pending' ) {
            it.value = '正常'
          }
          if (it.item.dataType === "number") {
            let arr = it.item.candidateValueArr.sort((a,b)=>a-b)
            it.item.candidateValueArr = [Number(arr[0]), Number(arr[arr.length - 1])]
          }
          return it
        })
      })
    })

    // 巡检类型
    request.get(urls.inspectionTypeDictUrl(), function(data){
      const obj ={}
      data.forEach(element => {
        obj[element.code] = element.text
      });
      that.setData({
        inspectionType:obj
      })
    })
  },
  // 获取巡检项名称
  getInspectionTypeName(code) {
    const {inspectionTypeList} =this.data
    const one=inspectionTypeList.find(e =>e.code == code)
    return one ? one.text:''
  },
  // 输入框发生变化
  onItemInput(e) {
    this.setData({
      [e.target.dataset.field]: e.detail.value,
    });
  },
  // 清除输入框
  onClear(e) {
    this.setData({
      [e.target.dataset.field]: '',
    });
  },
  // 选择值的时候发生变化
  radioChange(e) {
    this.setData({
      ['inspectionTaskItem['+e.target.dataset.field+'].value'] :  e.detail.value,
      ['inspectionTaskItem['+e.target.dataset.field+'].reason'] :  ''
    });
  },
  numInput(e){
    this.setData({
      ['inspectionTaskItem['+e.target.dataset.field+'].value'] :  e.detail.value
    });
  },
  numClear(e){
    this.setData({
      ['inspectionTaskItem['+e.target.dataset.field+'].value'] : ''
    });
  },
  forInput(e){
    this.setData({
      ['inspectionTaskItem['+e.target.dataset.field+'].reason'] :  e.detail.value
    });
  },
  forClear(e){
    this.setData({
      ['inspectionTaskItem['+e.target.dataset.field+'].reason'] : ''
    });
  },
  // 表单校验
  validData(formData) {
    const values = formData.inspectionTaskItem.filter(e => !e.value )
    if(values.length > 0 ) {
      const content = values.map(e => e.item.item).join()
      alert(content + '没有填写巡检结果')
      return false
    }
    return true
  },
   onSubmit() {
    const that = this
    const {
      id,
      code,
      startTime,
      endTime,
      cronExp,
      executorCode,
      executorName,
      unitCode,
      problems,
      solveSituation,
      status,
      type,
      inspectionTaskItem
    } = this.data

    const formData = {
      id,
      code,
      startTime,
      endTime,
      cronExp,
      executorCode,
      executorName,
      unitCode,
      problems,
      solveSituation,
      status,
      type,
      inspectionTaskItem
    }
   
    // if(!that.validData(formData)) {
    //   return
    // }

    dd.showLoading({
      content: '提交中...',
    })
    request.put(urls.inspectionServiceBeforeUrl(id), formData, () => {
      dd.hideLoading()
      dd.showToast({
        type: 'success',
        content: '提交成功',
        duration: 2000,
        success: () => {
          dd.redirectTo({
            url: '/pages/inspection/insurance/insurance?id=' + that.data.sId
            // url: '/pages/index/index'
          })
        },
      })
    }, () => {
      dd.hideLoading()
      dd.showToast({
        type: 'fail',
        content: '提交失败',
        duration: 2000,
      })
    })
  },
  back(){
    const that = this
    dd.redirectTo({
      url: '/pages/inspection/insurance/insurance?id=' + that.data.sId
    })
  }
});
